﻿using NRules.RuleModel;
using NRules.RuleModel.Builders;
using phminarm.expert.Model;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Runtime.InteropServices;
using System.Text;

namespace phminarm.expert
{
    public class DiagnosisRuleRepository : IRuleRepository
    {
        private readonly IRuleSet _ruleSet = new RuleSet("MyRuleSet");

        public IEnumerable<IRuleSet> GetRuleSets()
        {
            return new[] { _ruleSet };
        }

        public void LoadRules()
        {
            //Assuming there is only one rule in this example
            var rule = BuildRule();
            _ruleSet.Add(new[] { rule });
        }

        private IRuleDefinition BuildRule()
        {
            //Create rule builder
            var builder = new RuleBuilder();
            builder.Name("TestRule");

            //Build conditions
            PatternBuilder textPattern = builder.LeftHandSide().Pattern(typeof(TextModel), "text");

            Expression<Func<TextModel, bool>> cd1 =
                x => x.text.Equals("吃坏东西");
            textPattern.Condition(cd1);

            //Build actions
            Expression<Action<IContext, TextModel>> ac1
                = (ctx, x) => Console.WriteLine(":导致拉肚子");
            builder.RightHandSide().Action(ac1);

            //Build rule model
            return builder.Build();
        }
    }
}
